package com.jorado.trace;

import brave.Tracing;
import brave.sampler.CountingSampler;
import brave.sampler.Sampler;

public class ZipkinContext {

    public static ZipkinContext inst = new ZipkinContext();

    public static void startZipkin(float sampleRate, String projectName) {
        // 本地log模式
        if (ZipkinContext.inst.isInit()) {
            return;
        }
        ZipkinContext.inst.setSampleRate(sampleRate);
        ZipkinContext.inst.init(projectName);
        ZipkinContext.inst.setInit(true);
    }

    // 是否已经初始化
    public boolean isInit = false;

    private Tracing tracing;
    // 采样率
    private float sampleRate = 1.0f;
    // 采样率判别函数
    private Sampler sampler;

    public void init(String projectName) {
        // 默认采用的是log的方式
        Platform reporter = Platform.get();
        this.sampler = CountingSampler.create(sampleRate);
        this.tracing = Tracing.newBuilder().localServiceName(projectName).sampler(this.sampler).traceId128Bit(true)
                .reporter(reporter).build();
    }

    public Tracing getTracing() {
        return tracing;
    }

    public void setTracing(Tracing tracing) {
        this.tracing = tracing;
    }

    public float getSampleRate() {
        return sampleRate;
    }

    public void setSampleRate(float sampleRate) {
        this.sampleRate = sampleRate;
    }

    public Sampler getSampler() {
        return sampler;
    }

    public boolean isInit() {
        return isInit;
    }

    public void setInit(boolean isInit) {
        this.isInit = isInit;
    }

}
